var str = '', i, j, yy, mm, openbound, callback;
var data;
var zh_from = '';
var zh_to = '';
var curday, today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);

function parseDate(s) {
	var reg = new RegExp("[^0-9-]", "");
	if (s.search(reg) >= 0) {
		return today;
	}
	var ss = s.split("-");
	if (ss.length != 3) {
		return today;
	}
	if (isNaN(ss[0]) || isNaN(ss[1]) || isNaN(ss[2])) {
		return today;
	}
	return new Date(parseFloat(ss[0]), parseFloat(ss[1]) - 1, parseFloat(ss[2]));
}
//ajax获取data
function loadData() {
	var postData = JSON.parse(sessionStorage.getItem('jiageriliData'));
	var sys_d;
	$.ajax({
		type:"post",
		url:'../flight/search/calender?temp='+Math.random(),
		async:false,
		contentType:'application/json;charset=utf-8',
		data:JSON.stringify(postData),
		dataType:"json",
		success:function(res){
			if(res.status=="1001"){
				sys_d = res.data;
				$(".chufa_daoda").text(sys_d.startCityCode+"-"+sys_d.endCityCode);
				console.log(sys_d)
				return sys_d;
			}else{
				layer.alert("价格数据请求失败");				
			}			
		},
		error:function(err){
			layer.alert("请求失败");
		}
	});
	/*sys_d = {
		"startCityCode": "SHA",
		"endCityCode": "PEK",
		"startDate": "2017-07-20",
		"07-19": {
			"price": 501
		},
		"07-05": {
			"price": 497
		},
		"07-23": {
			"price": 556
		},
		"06-25": {
			"price": 570
		},
		"07-06": {
			"price": 497
		},
		"07-20": {
			"price": 570
		},
		"06-27": {
			"price": 556
		},
		"07-29": {
			"price": 556
		},
		"07-30": {
			"price": 556
		},
		"07-28": {
			"price": 556
		},
		"07-26": {
			"price": 556
		},
		"07-24": {
			"price": 556
		},
		"07-17": {
			"price": 556
		},
		"08-26": {
			"price": 556
		},
		"08-24": {
			"price": 556
		},
		"08-17": {
			"price": 556
		}
	}*/
	return sys_d;
}

function initCalendar() {
	console.log(loadData())
	var sysData = loadData();
	if(!sysData) return false;
	console.log(sysData)
	curday = new Date(sysData.startDate.replace(/-/g,"/"));
	data = sysData;
	console.log(sysData)
	var tmpy = curday.getFullYear();
	var tmpm = curday.getMonth();
	showmm = tmpm + 1;
	ajaxhq(tmpy, showmm, "1");
}
function ajaxhq(y, m, type) {
	cury = y;
	curm = m;
	if (m == 13) {
		curm = 1;
		cury = y + 1;
	}
	startdate = cury + (curm > 9 ? "-" + curm : "-0" + curm) + "-" + "01";
	if (type == null) {
		var str = "startdate=" + startdate;
		var sysData = loadData(str);
		data = sysData;
	}

	if (10 != startdate.length) {
		var t_date_str = startdate.split('-');
		var f_date_str = [];
		if (3 == t_date_str.length) {
			for (var i = 0, len = t_date_str.length; i < len; i++) {
				if (2 == t_date_str[i].length || 4 == t_date_str[i].length) {
					f_date_str.push(t_date_str[i]);
				} else if (1 == t_date_str[i].length) {
					f_date_str.push('0' + t_date_str[i]);
				}

			}
		}
		startdate = f_date_str.join('-');
	}

	zh_from = y;
	zh_to = m;
	drawTitle(y, m - 1);
	drawCalendar(y, m - 1, "b", data);
}
function drawTitle(y, m) {
	if (y > today.getFullYear()
			|| (y == today.getFullYear() && m >= today.getMonth() + 1)) {
		document.getElementById('pre').innerHTML = '<a class="iconfont" href=\'javascript:ajaxhq(yy,mm)\'>&#xe618;</a>';
	} else {
		document.getElementById('pre').innerHTML = '';
	}
	if((y == today.getFullYear()&&m>=today.getMonth() + 5)||(y > today.getFullYear()&&((Math.abs(today.getMonth()+1-m) % 12)<=5))){
		document.getElementById('next').innerHTML = '';
	}else{
		document.getElementById('next').innerHTML = '<a class="iconfont" href=\'javascript:ajaxhq(yy,mm+2)\'>&#xe898;</a>';
	}
	var showx = new Date(y, m, 1);
	var showy = showx.getFullYear();
	var showmm = showx.getMonth() + 1;
	if (showmm == 0) {
		showmm = 12;
		showy = new Date(y - 1, m, 1).getFullYear();
	}
	document.getElementById("title").innerHTML = showy + "年"
			+ (showmm > 9 ? showmm : "0" + showmm) + "月";
}
function drawCalendar(y, m, id, hqjson, startdate) {
	if (startdate != null) {
		var dateArr = startdate.split('-');
		startdate = new Date(parseInt(dateArr[0], 10), parseInt(dateArr[1] - 1,
						10), parseInt(dateArr[2] - 1), 10);
	} else {
		startdate = today;
	}
	var cdate;
	if (today.getTime() < startdate.getTime()) {
		cdate = startdate;
	} else {
		cdate = today;
	}
	var x = new Date(y, m, 1);
	var lowest = 100000;
	var lowobj = new Array();
	var mv = x.getDay();
	var d = x.getDate();
	yy = x.getFullYear();
	mm = x.getMonth();
	for (var i = 1; i <= mv; i++) {
		de = document.getElementById(id + i);
		de.innerHTML = "";
		de.bgColor = "";
	}
	showm = mm + 1;
	showprice = "";
	priceHtml = "";
	febclass = "";
	while (x.getMonth() == mm) {
		de = document.getElementById(id + (d + mv));
		$(de).attr('style', '');
		showdate = (showm > 9 ? showm : "0" + showm) + "-"
				+ (d > 9 ? d : "0" + d);
		priceHtml = '<span class="dianjichakan" style="color:#ababab">点击查看</span><br/>';
		if (hqjson) {
			if (hqjson[showdate]) {
				if (hqjson[showdate]["price"]) {
					var showprice = hqjson[showdate]["price"];
					if (lowobj[showprice])
						lowobj[showprice].push(id + (d + mv));
					else
						lowobj[showprice] = [id + (d + mv)];

					if (lowest > parseInt(showprice)) {
						lowest = parseInt(showprice);
					}
					priceHtml = "<span class='dianjichakan'><strong>¥<span style='font-size:16px;'>" + showprice
							+ "</span></strong></span><br />";
				}
			}
		}

		var str_1 = x.getFullYear()
				+ "-"
				+ (x.getMonth() + 1 > 9 ? x.getMonth() + 1 : "0"
						+ (x.getMonth() + 1)) + "-"
				+ (x.getDate() > 9 ? x.getDate() : "0" + x.getDate())+"|"+hqjson.startCityCode+"|"+hqjson.endCityCode;
		if (x.getFullYear() == today.getFullYear()
				&& x.getMonth() == today.getMonth()
				&& x.getDate() == today.getDate()) {
			if (today.getTime() >= startdate.getTime() || startdate == null) {
				de.innerHTML = "<a  class=\"cal_link\" _logname=\"calendar_list\" _logvalue='0' href='#' onclick='toAction(\""+str_1+"\")'>"
						+ priceHtml
						+ "<span class='moveDate' style='color:red;'>"
						+ showdate.slice(showdate.length-2,showdate.length)
						+ "</span></a>";
			} else {
				de.innerHTML = "<span class='Noprice moveDate'>" + showdate.slice(showdate.length-2,showdate.length) + "</span>";
			}
		} else if (x.getTime() < cdate.getTime()) {
			if (openbound) {
				de.innerHTML = "<a  class=\"cal_link\" _logname=\"calendar_list\" _logvalue='0' href='#' onclick='toAction(\""+str_1+"\")' class=bt>"
						+ priceHtml + "<span class='grey'>" + d + "</span></a>";
			} else {
				de.innerHTML = "<span class='Noprice moveDate'>" + showdate.slice(showdate.length-2,showdate.length) + "</span>";
			}
		} else {
			de.innerHTML = "<a  class=\"cal_link\" _logname=\"calendar_list\" _logvalue='0' href='#' onclick='toAction(\""
					+ str_1
					+ "\")' class="
					+ febclass
					+ ">"
					+ priceHtml
					+ "<span class='grey moveDate'>" + showdate.slice(showdate.length-2,showdate.length) + "</span></a>";
		}
		x.setDate(++d);
	}
	while (d + mv <= 42) {
		de = document.getElementById(id + (d + mv));
		de.innerHTML = "<span class='Noprice'>&nbsp;</span>";
		de.bgColor = "";
		d++;
	}
	if (lowobj[lowest] && lowobj[lowest].length > 0) {
		var z_temp = lowobj[lowest];
		for (var zzz in z_temp) {
			var zh_obj = document.getElementById(z_temp[zzz]);
			if (zh_obj.firstChild)
				zh_obj.firstChild.setAttribute('_logvalue', 1);
			zh_obj.innerHTML = zh_obj.innerHTML.replace(/<span>/i,
					'<span class="di_sp"><span class="di_img"></span>')
					.replace(/referprice/, 'lowest=1&referprice');
		}
	}

}
function setDate(d, id) {
	var dstr;
	if (id == "b") {
		if (mm >= 9) {
			if (mm > 11) {
				dstr = (yy + 1) + "-0" + (mm + 1 - 12) + "-" + d;
			} else {
				dstr = yy + "-" + (mm + 1) + "-" + d;
			}
		} else
			dstr = yy + "-0" + (mm + 1) + "-" + d;
	}
	if (callback && callback.length > 0) {
		callback(dstr);
	}
}

$(function() {
	var htmlStr = "";
	for (i = 0; i < 6; i++) {
		htmlStr += "<tr>";
		for (j = 1; j <= 7; j++) {
			htmlStr += "<td align='center' valign='middle' id=b"
					+ (i * 7 + j) + ">&nbsp;</td>";
		}
		htmlStr += "</tr>";
	}
	$("#html_div").html(htmlStr);
	initCalendar();
});

var toAction = function(strinfo) {
	alert(strinfo);
};
